package de.dhbw.asa.controller;

import java.sql.DriverManager;
import java.sql.ResultSet;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;


import de.dhbw.asa.model.Login;

@ManagedBean
@RequestScoped
public class LoginCtrl extends Login{

	@ManagedProperty(value = "#{login}")
	private Login theLogin;
	private String dbusername;
	private String dbpassword;

	java.sql.Connection con;
	java.sql.Statement ps;
	ResultSet rs;
	String SQL_Str;

	public void dbData(String userName) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/webshop", "root", "root");
			ps = con.createStatement();
			SQL_Str = "Select * from pwtabelle where userName like ('" + userName
					+ "')";
			rs = ps.executeQuery(SQL_Str);
			rs.next();
			dbusername = rs.getString("userName");
			dbpassword = rs.getString("userPassword");
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("Exception Occur :" + ex);
		}
	}
	
    public String checkPW()
    {
        dbData(getsUserName());

        if(getsUserName().equalsIgnoreCase(dbusername))
        {

            if(getsUserPassword().equals(dbpassword))
                return "products";
            else
            {
                return "failure";
            }
        }
        else
        {
            return "failure";
        }
    }
	
	

	public String getDbusername() {
		return dbusername;
	}

	public void setDbusername(String dbusername) {
		this.dbusername = dbusername;
	}

	public String getDbpassword() {
		return dbpassword;
	}

	public void setDbpassword(String dbpassword) {
		this.dbpassword = dbpassword;
	}

	public Login getTheLogin() {
		return theLogin;
	}

	public void setTheLogin(Login theLogin) {
		this.theLogin = theLogin;
	}

}
